package com.example.a10928.weifriend.ui.home;

import android.databinding.ViewDataBinding;
import android.view.KeyEvent;
import android.webkit.WebSettings;

import com.example.a10928.weifriend.R;
import com.example.a10928.weifriend.databinding.ActivityWebViewBinding;
import com.example.a10928.weifriend.http.api.VariableName;
import com.example.a10928.weifriend.ui.BaseActivity;
import com.example.a10928.weifriend.ui.DatabindingBaseActivity;

public class WebViewActivity extends DatabindingBaseActivity {

    public final static String DADA_WEBVIEW_URL = "dada_webview_url";
    public final static String DADA_WEBVIEW_TITLE = "dada_webview_title";
    public final static int SOURCE_MAKE_MONEY = 1010;
    public final static int SOURCE_NEW_GUIDE = 1011;
    public final static int SOURCE_UNIONPAY= 1012;
    public final static int SOURCE_CAR_MALL= 1013;
    public final static int SOURCE_FIND= 1014;
    private ActivityWebViewBinding mBinding;

    @Override
    protected void bindingInitData() {

    }

    @Override
    protected void bindingInitView(ViewDataBinding viewDataBinding) {
        mBinding = (ActivityWebViewBinding) viewDataBinding;
        int titleType = getIntent().getIntExtra(DADA_WEBVIEW_TITLE, -1);
        switch (titleType) {
            default:
            case SOURCE_MAKE_MONEY:
                setTitleText(ThemeEnum.Back_Default,"如何赚佣");
                break;
            case SOURCE_NEW_GUIDE:
                setTitleText(ThemeEnum.Back_Default,"新手指引");
                break;
            case SOURCE_UNIONPAY:
                setTitleText(ThemeEnum.Back_Default,"银联支付");
                break;
            case SOURCE_CAR_MALL:
                setTitleText(ThemeEnum.Back_Default,"汽车商城");
                break;
                case SOURCE_FIND:
                setTitleText(ThemeEnum.Back_Default,"发现");
                break;
        }
        mBinding.wv.getSettings().setJavaScriptEnabled(true);

        /**
         * LOAD_CACHE_ONLY: 不使用网络，只读取本地缓存数据
         * LOAD_DEFAULT: （默认）根据cache-control决定是否从网络上取数据。
         * LOAD_NO_CACHE: 不使用缓存，只从网络获取数据.
         * LOAD_CACHE_ELSE_NETWORK，只要本地有，无论是否过期，或者no-cache，都使用缓存中的数据。
         */
        mBinding.wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);//不使用缓存，只从网络获取数据.
        //支持屏幕缩放
        mBinding.wv.getSettings().setSupportZoom(true);
        mBinding.wv.getSettings().setBuiltInZoomControls(true);

        mBinding.wv.setWebViewClient(new com.tencent.smtt.sdk.WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(com.tencent.smtt.sdk.WebView webView, String s) {
                mBinding.wv.loadUrl(s);
                return true;
            }
        });

        String url = getIntent().getStringExtra(DADA_WEBVIEW_URL);
        String data=getIntent().getStringExtra(VariableName.DATA);
        if (url != null) {
            mBinding.wv.loadUrl(url);
        }else {
         //   data="<!DOCTYPE html>\\n<html lang=\\\"en\\\">\\n<head>\\n    <meta http-equiv=\\\"Content-Type\\\" content=\\\"text\\/html; charset=UTF-8\\\">\\n    <meta name=\\\"viewport\\\" content=\\\"initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no\\\">\\n    <meta name=\\\"format-detection\\\" content=\\\"telephone=no, email=no\\\">\\n    <title>\\u652f\\u4ed8\\u8df3\\u8f6c\\u4e2d....<\\/title>\\n    <style>\\n        body {\\n            font-size: 16px;\\n            text-align:center;\\n            padding-top: 40px;\\n        }\\n    <\\/style>\\n    <script>\\n        window.onload = function () {\\n            var action = document.redirectForm.action;\\n            document.redirectForm.submit();\\n        }\\n    <\\/script>\\n<\\/head>\\n<body>\\n<div class=\\\"block\\\">\\n    <form name = \\\"redirectForm\\\" action=\\\"http:\\/\\/180.168.61.86:27380\\/hpayTransGatewayWeb\\/trans\\/QuickPreDebit.htm\\\" method=\\\"POST\\\" target=\\\"_self\\\">\\n        \\n            <input type=\\\"hidden\\\" name=\\\"accNo\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"backUrl\\\" value=\\\"http:\\/\\/channel.test.maokung.com\\/r\\/ra\\/channel\\/hanyin\\/unionH5Notify\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"bankCode\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"currencyCode\\\" value=\\\"156\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"encoding\\\" value=\\\"UTF-8\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"frontUrl\\\" value=\\\"http:\\/\\/channel.test.maokung.com\\/r\\/ra\\/channel\\/hanyin\\/unionH5Return\\/UWTF2018120710565554951\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"goodsDesc\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"goodsName\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"hpMerCode\\\" value=\\\"YMHKJZFCSYMH@20180725111551\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"idNo\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"idType\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"insCode\\\" value=\\\"80000385\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"insMerchantCode\\\" value=\\\"887581298600447\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"ledger\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"merReserved\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"name\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"nonceStr\\\" value=\\\"c75248498bc64600b546985129893d7b\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"orderAmount\\\" value=\\\"1\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"orderDate\\\" value=\\\"20181207\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"orderNo\\\" value=\\\"UWTF2018120710565554951\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"orderTime\\\" value=\\\"20181207105655\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"orderTimeOut\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"productCode\\\" value=\\\"10000007\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"requestFlag\\\" value=\\\"web\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"signType\\\" value=\\\"MD5\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"signature\\\" value=\\\"BC51F1CD7C2697BFF317ADAD37516B79\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"telNo\\\" value=\\\"\\\"> <br\\/>\\n        \\n            <input type=\\\"hidden\\\" name=\\\"version\\\" value=\\\"1.0.0\\\"> <br\\/\\n        \\n    <\\/form>\\n<\\/div>\\n<\\/body>\\n<\\/html>\"";
            mBinding.wv.loadDataWithBaseURL(null, data, "text/html", "utf-8", null);
        }

    }

    @Override
    protected int bindingGetContentView() {
        return R.layout.activity_web_view;
    }

    //处理WebView返回

    //使用Webview的时候，返回键没有重写的时候会直接关闭程序，这时候其实我们要其执行的知识回退到上一步的操作
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //这是一个监听用的按键的方法，keyCode 监听用户的动作，如果是按了返回键，同时Webview要返回的话，WebView执行回退操作，因为mWebView.canGoBack()返回的是一个Boolean类型，所以我们把它返回为true
        if (keyCode == KeyEvent.KEYCODE_BACK && mBinding.wv != null && mBinding.wv.canGoBack()) {
            mBinding.wv.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
}
